package com.yiren.dbaa.module.practice_set.map.presenter;

import android.app.Activity;

import com.google.gson.Gson;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.map.contract.BindLineContract;
import com.yiren.dbaa.module.practice_set.map.model.BindLineModel;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Describe
 *
 * @author WRX
 * @date 2022/12/26
 */
public class BindLinePresenter implements BindLineContract.Presenter {

    private Activity activity;
    private BindLineContract.Model bindLineModel;

    public BindLinePresenter(Activity activity) {
        this.activity = activity;
        bindLineModel = new BindLineModel(activity);
    }

    @Override
    public void loadUserAllLine(String siteCode, BindLineContract.View v) {
        LoadDialog.show(activity, "正在获取线路信息...");
        bindLineModel.getUserAllLine(new CommonCallBack() {
            @Override
            public void success(Object result) {
                List<LineInfoEntity> lineInfos = (List<LineInfoEntity>) result;
                //将线路按考场进行分组
                Map<String, List<LineInfoEntity>> map = new HashMap<>();
                for (int i = 0; i < lineInfos.size(); i++) {
                    String siteCode = lineInfos.get(i).getSiteCode();
                    if (map.containsKey(siteCode)) {
                        map.get(siteCode).add(lineInfos.get(i));
                    } else {
                        List<LineInfoEntity> list = new ArrayList<>();
                        list.add(lineInfos.get(i));
                        map.put(siteCode, list);
                    }
                }
                //将当前考场线路排在首位
                LinkedHashMap<String, List<LineInfoEntity>> linkedHashMap = new LinkedHashMap<>();
                if (map.containsKey(siteCode)) {
                    linkedHashMap.put(siteCode, map.get(siteCode));
                }
                for (Map.Entry<String, List<LineInfoEntity>> entry : map.entrySet()) {
                    if (!entry.getKey().equals(siteCode)) {
                        linkedHashMap.put(entry.getKey(), entry.getValue());
                    }
                }
                //添加考场名称进入list
                List<Object> list = new ArrayList<>();
                for (Map.Entry<String, List<LineInfoEntity>> entry : linkedHashMap.entrySet()) {
                    List<LineInfoEntity> value = entry.getValue();
                    if (null != value && value.size() > 0) {
                        list.add(value.get(0).getSiteName());
                        list.addAll(value);
                    }
                }
                v.getLineInfoSuccess(list);
                LoadDialog.dismiss(activity);
            }

            @Override
            public void fail(String error) {
                LoadDialog.dismiss(activity);
                MessageBox.show(activity, error);
            }
        });
    }

}